package com.example.a10928.weifriend.ui.rank_list;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.a10928.weifriend.R;
import com.example.a10928.weifriend.adapter.MyViewPageAdapter;
import com.example.a10928.weifriend.http.api.VariableName;
import com.example.a10928.weifriend.ui.BaseFragment;
import com.example.a10928.weifriend.util.Utils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.lang.reflect.Field;
import java.util.ArrayList;

import static com.vector.update_app.utils.AppUpdateUtils.getDisplayMetrics;

public class RankListFragment extends BaseFragment {

    TabLayout tabLayout;
    MagicIndicator indicator;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_rank_list,container,false);
        ArrayList<String> titleData=new ArrayList<>();
        titleData.add("昨日排行榜");
        titleData.add("今日排行榜");
        titleData.add("本周排行榜");

        ArrayList<Fragment> fragments=new ArrayList<>();

        RankFragment one_f=new RankFragment();
        Bundle one_bundle=new Bundle();
        one_bundle.putInt(VariableName.TYPE,0);
        one_f.setArguments(one_bundle);

        RankFragment two_f=new RankFragment();
        Bundle two_bundle=new Bundle();
        two_bundle.putInt(VariableName.TYPE,1);
        two_f.setArguments(two_bundle);

        RankFragment three_f=new RankFragment();
        Bundle three_bundle=new Bundle();
        three_bundle.putInt(VariableName.TYPE,2);
        three_f.setArguments(three_bundle);

        fragments.add(one_f);
        fragments.add(two_f);
        fragments.add(three_f);

        MyViewPageAdapter adapter=new MyViewPageAdapter(((AppCompatActivity)getActivity()).getSupportFragmentManager(),titleData,fragments);
        ViewPager vp=(ViewPager)view.findViewById(R.id.vp);
        tabLayout=(TabLayout)view.findViewById(R.id.tablayout);
        vp.setAdapter(adapter);
        tabLayout.setupWithViewPager(vp);
        tabLayout.setTabsFromPagerAdapter(adapter);


        indicator=(MagicIndicator)view.findViewById(R.id.indicator);
        indicator.setBackgroundResource(R.color.main);

        CommonNavigator commonNavigator=new CommonNavigator(getActivity());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titleData.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int i) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setWidth(Utils.dp2px(getActivity(),120));
                simplePagerTitleView.setNormalColor(Color.WHITE);
                simplePagerTitleView.setSelectedColor(Color.WHITE);
                simplePagerTitleView.setText(titleData.get(i));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vp.setCurrentItem(i);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setColors(Color.WHITE);
                return linePagerIndicator;
            }
        });
        indicator.setNavigator(commonNavigator);
        LinearLayout linearLayout=commonNavigator.getTitleContainer();
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        ViewPagerHelper.bind(indicator, vp);






        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

}
